package zhou.contest12;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class AccountController {
	
	//用户创建资金账户：
	public boolean addAccount(User u){
    	BaseDao dao=new BaseDao();
    	Connection con=dao.getCon();
    	String sql="insert into account (id,name,pwd) value('"+u.getUserId()+"','"+u.getUsername()+"','"+u.getUserpwd()+"')";
    	
    	try{		
    	PreparedStatement ps=con.prepareStatement(sql);
    	ps.executeUpdate();
    	if(ps!=null){
    		ps.close();    		   		
    	}
    	if(con!=null){
    		con.close();    		   		
    	}		
    			
    }  catch(SQLException e){
    	e.printStackTrace();
    	}
		return true;
    }
	
	//用户存钱:
	public boolean deposit(User u,double money) {		
		BaseDao bd=new BaseDao();
		Connection con=bd.getCon();		
		String sql="update account set balance =balance+? where name='"+u.getUsername()+"'and pwd='"+u.getUserpwd()+"'";
			   
		try {
		PreparedStatement ps=con.prepareStatement(sql);  			
		ps.setDouble(1, money);
				
		ps.execute();
		if(ps!=null){
		    ps.close(); 		   		
		}
		if(con!=null){
		 con.close();    		   		
	}
		    	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		   		   
			   
			   return true;
		   } 
	
	//用户取钱:
		public boolean withdraw(User u,double money) {		
			BaseDao bd=new BaseDao();
			Connection con=bd.getCon();	
			String sql1="select balance from account where name='"+u.getUsername()+"','"+u.getUserpwd()+"'";
			String sql2="update account set balance =balance-? where name='"+u.getUsername()+"'and pwd='"+u.getUserpwd()+"'";
				   
			try {
			PreparedStatement ps1=con.prepareStatement(sql1); 
			ResultSet rs1=ps1.executeQuery();
			
			if(rs1.getDouble("balance")>=money){
				PreparedStatement ps2=con.prepareStatement(sql2); 
	    		ps2.setDouble(1, money);					
			    ps2.executeUpdate();			   
			    if(ps2!=null){
				    ps2.close(); 		   		
				}
	    	}
			else{
				System.out.println("对不起，账户余额不足！");
			}
			
			if(rs1!=null){
			    rs1.close(); 		   		
			}
			if(ps1!=null){
			    ps1.close(); 		   		
			}
			if(con!=null){
			 con.close();    		   		
		}
			    	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		   		   				   
				   return true;
} 
	
	//用户可以转账到其他人的账户：
	public void transferAccounts(User u1,User u2,double money){
		AccountController ac=new AccountController();
		ac.deposit(u1,money);
		ac.withdraw(u2,money);
		System.out.println("转账成功");
		}
	
	//用户注销资金账户:
	public boolean delete(User u){
    	BaseDao dao=new BaseDao();
    	Connection con=dao.getCon();
    	String sql="delete from account where name='"+u.getUsername()+"' and pwd='"+u.getUserpwd()+"'";
    	
    	  try{		
    	  PreparedStatement ps=con.prepareStatement(sql);
    	
    	  ps.executeUpdate();    
    	  if(ps!=null){
    		ps.close();    		   		
    	  }
    	  if(con!=null){
    		con.close();    		   		
    	  }		
    			
          }catch(SQLException e){
    	     e.printStackTrace();
    	}
    	return true;
    }
	
	
}


